package com.lixinlei.security.auth.oauth2.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 正真的业务实现，这里的用户来源一定是要读数据库的，这里目前是硬编码了一个用户
     * @param username
     * @return UserDetails 封装了 Spring 所需要的用户信息
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return User
                .withUsername(username)
                .password(passwordEncoder.encode("123456")) // 模拟从数据库里拿出来的密码都是密文的
                .authorities("ROLE_ADMIN") // 理解成角色
                .build();
    }

}
